﻿using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections.Generic;

public class ExportPanelHierarchy
{
    private static System.Type[] ms_componentTypes = {
        typeof(LuaBehaviour),
        typeof(Button),
        typeof(SoarDInput),
        typeof(InputField),
        typeof(SoarDScrollRect),
        typeof(SoarDAutoScroll),
        typeof(ScrollRect),
        typeof(Dropdown),
        typeof(Image),
        typeof(RawImage),
        typeof(Scrollbar),
        typeof(Slider),
        typeof(SoarDText),
        typeof(Text),
        typeof(Toggle),
        typeof(GridLayoutGroup),
        typeof(LayoutElement),
        typeof(CanvasGroup),
        typeof(ToggleGroup),
        typeof(TextMesh),
        typeof(FxExportItem),
        typeof(Animation),
    };

    static Object GetChildComponent(GameObject go)
    {
        Object component = null;
        for (int i = 0; i < ms_componentTypes.Length; ++i)
        {
            component = go.GetComponent(ms_componentTypes[i]);
            if (component != null)
            {
                break;
            }
        }
        return component;
    }

    public static void ExportAll()
    {
        string[] arrObjNames = AssetDatabase.FindAssets("t:GameObject");

        for (int i = 0; i < arrObjNames.Length; i++)
        {
            string assetPath = AssetDatabase.GUIDToAssetPath(arrObjNames[i]);
            GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
            if (obj != null)
            {
                ExportSingle(obj, false);
            }
        }
        AssetDatabase.SaveAssets();
    }


    public static void ExportSelect(Object obj)
    {
        GameObject root = obj as GameObject;
        if (root == null) return;

        ExportSingle(root);
        AssetDatabase.SaveAssets();
    }

    private static void ExportSingle(GameObject root, bool bAdd = true)
    {
        UIHierarchy hierarchy = root.GetComponent<UIHierarchy>();
        if (bAdd && hierarchy == null)
        {
            hierarchy = root.AddComponent<UIHierarchy>();
        }
        else if (hierarchy == null)
        {
            return;
        }

        FxExportItem[] fxItems = root.GetComponentsInChildren<FxExportItem>(true);
        //        List<UIHierarchy.ItemInfo> fxFields = fxItems.Select(item => new UIHierarchy.ItemInfo(item.name, item)).ToList();
        List<UIHierarchy.ItemInfo> fxFields = new List<UIHierarchy.ItemInfo>();
        foreach (var item in fxItems)
        {
            fxFields.Add(new UIHierarchy.ItemInfo(item.name, item));
        }
        hierarchy.SetEffects(fxFields);

        UIExportItem[] items = root.GetComponentsInChildren<UIExportItem>(true);
        string info = string.Format("Export Panel: {0}, Field Count: {1}\n", root.name, items.Length);
        List<UIHierarchy.ItemInfo> fields = new List<UIHierarchy.ItemInfo>();
        foreach (var item in items)
        {
            string fieldName = string.IsNullOrEmpty(item.FieldName) ? item.name : item.FieldName;
            Object fieldItem = GetChildComponent(item.gameObject);
            if (fieldItem == null) fieldItem = item.transform;
            fields.Add(new UIHierarchy.ItemInfo(fieldName, fieldItem));

            info += string.Format("-- {0}\n", fieldName);
        }
        hierarchy.SetWidgets(fields);
        if (hierarchy.externals != null && hierarchy.externals.Count == 0 && fxFields.Count == 0 && fields.Count == 0)
        {
            Object.DestroyImmediate(hierarchy, true);
        }
        EditorUtility.SetDirty(root);
        Debug.Log(info);
    }

    //只导入新增的控件
    public static void ExportOnlyNewSelect(Object obj)
    {
        GameObject root = obj as GameObject;
        if (root == null) return;

        UIHierarchy hierarchy = root.GetComponent<UIHierarchy>() ?? root.AddComponent<UIHierarchy>();
        FxExportItem[] fxItems = root.GetComponentsInChildren<FxExportItem>(true);
        //        List<UIHierarchy.ItemInfo> fxFields = fxItems.Select(item => new UIHierarchy.ItemInfo(item.name, item)).ToList();
        List<UIHierarchy.ItemInfo> fxFields = new List<UIHierarchy.ItemInfo>();
        foreach (var item in fxItems)
        {
            fxFields.Add(new UIHierarchy.ItemInfo(item.name, item));
        }
        List<UIHierarchy.ItemInfo> newFx = new List<UIHierarchy.ItemInfo>();
        foreach (var itemNew in fxFields)
        {
            UIHierarchy.ItemInfo item = hierarchy.effects.Find(x => { return x.name == itemNew.name; });
            if (item == null)
            {
                newFx.Add(itemNew);
            }
        }
        hierarchy.effects.AddRange(newFx);

        UIExportItem[] items = root.GetComponentsInChildren<UIExportItem>(true);
        Debug.Log(string.Format("Export Panel: {0}, Field Count: {1}", root.name, items.Length));
        List<UIHierarchy.ItemInfo> fields = new List<UIHierarchy.ItemInfo>();
        foreach (var item in items)
        {
            string fieldName = string.IsNullOrEmpty(item.FieldName) ? item.name : item.FieldName;
            Object fieldItem = GetChildComponent(item.gameObject) ?? item.gameObject;
            fields.Add(new UIHierarchy.ItemInfo(fieldName, fieldItem));
        }
        List<UIHierarchy.ItemInfo> newField = new List<UIHierarchy.ItemInfo>();
        foreach (var itemNew in fields)
        {
            UIHierarchy.ItemInfo item = hierarchy.widgets.Find(x => x.name == itemNew.name);
            Debug.Log(item);
            if (item == null)
            {
                newField.Add(itemNew);
            }
        }
        hierarchy.widgets.AddRange(newField);

        if (hierarchy.externals != null && hierarchy.externals.Count == 0 && fxFields.Count == 0 && fields.Count == 0)
        {
            Object.DestroyImmediate(hierarchy, true);
        }
        EditorUtility.SetDirty(root);
        AssetDatabase.SaveAssets();
    }
}